package kk.yiyi.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.icu.text.LocaleDisplayNames;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    //网上请求下来的省名
    private List<String> provinceDatas=new ArrayList<>();
    //网上请求下来的市名
    private Map<String,ArrayList<String>> cityDatas=new HashMap<>();
    //网上请求下来的区名
    private Map<String,ArrayList<String>> areaDatas=new HashMap<>();

    //  弹窗所需省数据
    private List<ShengBean> options1Items = new ArrayList<ShengBean>();
    //  弹窗所需市数据
    private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();
    //  弹窗所需区数据
    private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //先拿数据，再去显示弹窗
        initView();
    }

    private void initView(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                SelectDataService selectDataService=new SelectDataService();
                selectDataService.selectData("1", new SelectListener() {
                    @Override
                    public void success(String result) {
                        Log.d("demo",result);
                        Gson gson=new Gson();
                        LocationBean locationBean=gson.fromJson(result, LocationBean.class);
                        if(locationBean.getContent()==null){
                            Log.d("demo","解析失败");
                        }else {
                            Log.d("demo","解析成功");
                        }
                        for(int i=0;i<locationBean.getContent().size();i++){
                            Log.d("xxx",String.valueOf(i));
                            String id=String.valueOf(locationBean.getContent().get(i).getId());
                            final String province=locationBean.getContent().get(i).getArea_name();
                            provinceDatas.add(province);
                            SelectDataService selectCity=new SelectDataService();
                            selectCity.selectData(id, new SelectListener() {
                                @Override
                                public void success(String result) {
                                    Gson gson1=new Gson();
                                    ArrayList<String> city=new ArrayList<>();
                                    LocationBean locationBean=gson1.fromJson(result, LocationBean.class);
                                    for(int i=0;i<locationBean.getContent().size();i++) {
                                        final String cityName=locationBean.getContent().get(i).getArea_name();
                                        String cityId=String.valueOf(locationBean.getContent().get(i).getId());
                                        city.add(cityName);
                                        Log.d("haha",cityId);
                                        SelectDataService selectArea=new SelectDataService();
                                        selectArea.selectData(cityId, new SelectListener() {
                                            @Override
                                            public void success(String result) {
                                                Gson gson2=new Gson();
                                                ArrayList<String> area=new ArrayList<>();
                                                LocationBean locationBean=gson2.fromJson(result, LocationBean.class);
                                                for(int i=0;i<locationBean.getContent().size();i++) {
                                                    String areaName=locationBean.getContent().get(i).getArea_name();
                                                    area.add(areaName);
                                                }
                                                areaDatas.put(cityName,area);
                                            }

                                            @Override
                                            public void fail(String error) {

                                            }
                                        });
                                    }
                                    cityDatas.put(province,city);

                                }

                                @Override
                                public void fail(String error) {

                                }
                            });
                        }

                        //遍历省
                        for(String item:provinceDatas){
                            options2Items.add(cityDatas.get(item));
                            ArrayList<String> city=cityDatas.get(item);
                            List<ShengBean.Shi> shi=new ArrayList<>();
                            //存放区
                            ArrayList<ArrayList<String>> province_AreaList = new ArrayList<>();
                            //遍历市
                            if(city!=null) {
                                for (String cityItem : city) {
                                    shi.add(new ShengBean.Shi(cityItem, cityDatas.get(cityItem)));
                                    province_AreaList.add(cityDatas.get(cityItem));
                                }
                            }
                            options1Items.add(new ShengBean(item,shi));
                            /**
                             * 添加城市数据
                             */
                            options2Items.add(city);
                            /**
                             * 添加地区数据
                             */
                            options3Items.add(province_AreaList);
                        }

                        for(String item:provinceDatas){
                            Log.d("demo",item);
                        }

//                showPickerView();
                    }

                    @Override
                    public void fail(String error) {
                        Toast.makeText(MainActivity.this,error,Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }).start();

    }


    /**
     * 展示选择器
     */
    private void showPickerView() {// 弹出选择器

        OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                final String tx = options1Items.get(options1).name +
                        options2Items.get(options1).get(options2) +
                        options3Items.get(options1).get(options2).get(options3);
                MainActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this,tx,Toast.LENGTH_SHORT).show();
                    }
                });

            }
        })

                .setTitleText("城市选择")
                .setDividerColor(Color.BLACK)
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(20)
                .build();

        /*pvOptions.setPicker(options1Items);//一级选择器
        pvOptions.setPicker(options1Items, options2Items);//二级选择器*/
        pvOptions.setPicker(options1Items, options2Items, options3Items);//三级选择器
        pvOptions.show();
    }
}
